<template>
	<view class="account_conversion">
		<form @submit="formSubmit" @reset="formReset">
			<view class="first">
				<view class="ccc" v-if="price.cash">{{price.cash}}
					<view class="title">余额</view>
				</view>
				<view class="ccc" v-else>0
					<view class="title">余额</view>
				</view>
				<view class="ccc" v-if="price.pointsDynamic">{{price.pointsDynamic}}
					<view class="title">鸡蛋</view>
				</view>
				<view class="ccc" v-else>0
					<view class="title">鸡蛋</view>
				</view>
				<view class="ccc" v-if="price.pointsStatic">{{price.pointsStatic}}
					<view class="title">健康分</view>
				</view>
				<view class="ccc" v-else>0
					<view class="title">健康分</view>
				</view>
				<view class="ccc" v-if="price.shoppingVoucher">{{price.shoppingVoucher}}
					<view class="title">消费券</view>
				</view>
				<view class="ccc" v-else>0
					<view class="title">消费券</view>
				</view>
				<view class="ccc" v-if="price.chickenNum">{{price.chickenNum}}
					<view class="title">匠心鸡</view>
				</view>
				<view class="ccc" v-else>0
					<view class="title">匠心鸡</view>
				</view>
			</view>
			<view class="second">
				<view class="uni-form-item uni-column">
					<view class="title flex">健&nbsp;康&nbsp;值：
					</view>
					<view class="uni-input">{{priceScore}}</view>
				</view>
				<view class="uni-form-item uni-column">
					<view class="title flex">转出类型：

					</view>
					<view @click="fuck(ddd)" v-for="(item,ddd) in list" class="uni-input"
						:class="typeIndex===ddd?'bac':''">{{item}}</view>
				</view>
				<view class="uni-form-item uni-column">
					<view class="title flex">转入类型：

					</view>
					<view @click="fuck1(bbb)" v-for="(item,bbb) in typeList" class="uni-input"
						:class="index===bbb?'bac':''">{{item}}</view>
				</view>
				<view class="uni-form-item uni-column">
					<view class="title flex">转换数量：

					</view>
					<input class="uni-input" type="number" v-model="sum" />
				</view>
			</view>
			<button class="uni-btn-v" type="primary" form-type="submit">提交</button>
		</form>
		<paypass ref="yspay" @cancel="formReset" @submit="handleClick"></paypass>
	</view>
</template>

<script>
	import paypass from "@/components/ys-paypass/ys-paypass.vue";
	export default {
		components: {
			paypass
		},
		data() {
			return {
				index: 0,
				typeIndex: 0,
				visible: false,
				payPassword: "",
				value: [],
				price: {},
				priceScore: 0,
				sum: "",
				typeList: ["消费券"],
				list: ["鸡蛋", "健康分", "匠心鸡", "余额"],
				GoodsDetail: {},
			}
		},
		onShow() {
			this.getInit()
			this.fuck(0)
		},
		// onLoad(e) {
		// 	this.$http({
		// 		url: '/crmStoreProduct/selCrmStoreProductDetailed?id=' + e.id,
		// 		method: 'POST',
		// 	}).then(res => {
		// 		this.GoodsDetail = res.data.data.list
		// 		console.log(this.GoodsDetail);
		// 	})
		// },
		methods: {
			fuck1(index) {
				this.index = index
			},
			getInit() {
				// 查询各类余额
				this.$http({
					url: '/member/getMemberAccount',
				}).then(res => {
					if (res.data.code === 200) {
						this.price = res.data.data
					} else if (res.data.code === 450) {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/Applogin/Applogin'
							})
						}, 1000)
					}
				})
				// 查询健康分价格
				this.$http({
					url: '/member/selectHealthScoreNum',
					method: 'GET'
				}).then(res => {
					if (res.data.code === 200) {
						this.priceScore = res.data.data.variableValue
					} else {}
				})
			},
			handleClick(ex) {
				let decreaseAccount = 1
				let increaseAccount = 1
				if (this.list[this.typeIndex] === "鸡蛋") {
					decreaseAccount = 2
				} else if (this.list[this.typeIndex] === "认养券") {
					decreaseAccount = 1
				} else if (this.list[this.typeIndex] === "健康分") {
					decreaseAccount = 3
				} else if (this.list[this.typeIndex] === "匠心鸡") {
					decreaseAccount = 6
				} else if (this.list[this.typeIndex] === "余额") {
					decreaseAccount = 4
				}


				if (this.typeList[this.index] === "消费券") {
					increaseAccount = 2
				} else if (this.typeList[this.index] === "健康分") {
					increaseAccount = 1
				} else if (this.typeList[this.index] === "认养券") {
					increaseAccount = 3
				} else if (this.typeList[this.index] === "鸡蛋") {
					increaseAccount = 6
				} else if (this.typeList[this.index] === "余额") {
					increaseAccount = 4
				}
				this.$http({
					url: '/member/accountExchange',
					method: 'GET',
					data: {
						decreaseAccount: decreaseAccount, // 转出 扣
						increaseAccount: increaseAccount, // 转入 加
						exchangeNum: this.sum, // 数量
						payPassword: ex.value // 支付密码
					}
				}).then(res => {
					if (res.data.code === 200) {
						uni.showToast({
							title: '转换成功',
						})
						this.visible = false
						this.sum = ""
						this.payPassword = ""
						this.getInit()
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}, 1000)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'error'
						})
					}
				})
			},
			formSubmit() {
				if (this.sum !== 0 && this.sum) {
					// this.visible = true
					uni.showModal({
						cancelText: "取消",
						confirmText: "确认",
						title: "确认兑换",
						content: '确认将<' + this.list[this.typeIndex] + '>转换成<' + this.typeList[this.index] +
							'>,数量为：' + this.sum,
						confirmColor: "#88A58A",
						cancelColor: "#222222",
						success: res => {
							if (res.confirm) {
								this.$refs.yspay.show();
							}
						}
					})
				} else {
					uni.showToast({
						title: '请填写数量',
						icon: 'error'
					})
				}


			},
			formReset() {
				this.visible = false
				this.payPassword = ""
			},
			fuck(e) {
				this.typeIndex = e
				if (this.typeIndex === 2) {
					this.typeList = ["消费券"]
					this.index = 0
				} else if (this.typeIndex === 1) {
					this.typeList = ["余额", "认养券"]
					this.index = 0
				} else if (this.typeIndex === 3 || this.typeIndex === 0) {
					this.typeList = ["消费券", "健康分", "认养券"]
					this.index = 0
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import './accountConversion.css'
</style>
